﻿using DataViewTool;
using DataViewTool.Models;
using Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication2.Models;

namespace WindowsFormsApplication2
{
    public partial class MainFrm : Form
    {
        MySqlHelper mysql = null;
        DataModel currentModel = null;
        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void leftTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtTableCommentOld.Text = txtTableComment.Text;
            txtTableNameOld.Text = txtTableName.Text;
            mainDataGridOld.DataSource = mainDataGrid.DataSource;
            TreeNode node = e.Node;
            TableInfo info = node.Tag as TableInfo;
            txtTableName.Text = info.TableName;
            txtTableComment.Text = info.TableComent;
            String sql = "SELECT column_name as columnname,data_type as datatype,column_comment as columncomment FROM information_schema.columns WHERE table_schema='"+currentModel.DataBase+"' AND table_name='"+info.TableName+"';";
            List<ColumnInfo> list = mysql.ExecuteObjects<ColumnInfo>(sql);
            mainDataGrid.DataSource = list;
        }

        private void LoadData() {
            string conStr = string.Format("Database={4};Data Source={0};User Id={1};Password={2};pooling=false;CharSet=utf8;port={3}", currentModel.Host, currentModel.UserName, currentModel.Password, currentModel.Port, currentModel.DataBase);
            mysql = new MySqlHelper(conStr);
            String sql = string.Format("SELECT table_name as tablename,table_comment as tablecoment FROM information_schema.tables WHERE table_schema='{0}' AND table_type='base table';", currentModel.DataBase);
            List<TableInfo> list = mysql.ExecuteObjects<TableInfo>(sql);
            list.Sort((TableInfo a, TableInfo b) =>
            {
                return a.TableName.CompareTo(b.TableName);
            });
            leftTree.Nodes.Clear();
            foreach (var item in list)
            {
                TreeNode node = new TreeNode();
                if (item.TableComent.IndexOf(";") < 0)
                {
                    item.TableComent = "NONE";
                }
                else
                {
                    item.TableComent = item.TableComent.Split(';')[0];
                }
                node.Tag = item;
                node.Text = item.TableName + "(" + item.TableComent + ")";
                leftTree.Nodes.Add(node);
            }
        }



        private void mainDataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView current = sender as DataGridView;
            String txt = current.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            Clipboard.SetDataObject(txt);
        }


        private void txtTableName_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TextBox txtBox = sender as TextBox;
            Clipboard.SetDataObject(txtBox.Text);
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            MyDataFrm frm = new MyDataFrm();
            frm.ShowDialog();
            BindData();
        }

        private void BindData()
        {
            DataComBox.DataSource = Program.DataMoudels.Models.ToList();
            DataComBox.DisplayMember = "Name";
            DataComBox.ValueMember = "Name";
        }

        private void DataComBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Name = DataComBox.Text;
            var model = Program.DataMoudels.Models.Find(m => m.Name == Name);
            if (model != null) {
                currentModel = model;
                LoadData();
            }
        }
    }
}
